package com.zac.coroutine.algorithm.leetcode.others

/**
 * author zac
 * date 2025/8/7
 *
 */
object GuessGame {

    @JvmStatic
    fun main(args: Array<String>) {
        print(guessNumber(2126753390))
    }

    fun guessNumber(n: Int): Int {
        if (n == 1) return 1
        var a: Int
        var x = n / 2
        var max = n
        var min = 0
        while (true) {
            a = guess(x)
            if (a == 0) return x
            if (a > 0) {
                // 目标数字在所选数字的右边
                min = x + 1
            }

            if (a < 0) {
                // 目标数字在所选数字的左边
                max = x-1
            }
            x = ((max.toLong() + min.toLong()) / 2L).toInt()

        }

    }

    fun guess(num: Int): Int {
        val a = 1702766719

        return if (num > a) -1
        else if (num < a) 1
        else 0

    }


}